const EnumLoginUserInfo = require('../constants/EnumCookie').EnumLoginUserInfo;
let fs = require('fs')

module.exports = class extends think.Controller {
    constructor(ctx) {
        super(ctx);
        this.cacheService = think.service('cache');
    }

    async __before() {
        // 去除服务来源
        this.ctx.remove('X-Powered-By');

        // 不在白名单中的路由检查是否登录
        // if (think.config('custom.noCheckIsLoginRoutes').indexOf(this.ctx.url) === -1 ){
        //     if (!this.checkIsLogin()) return this.fail(900, "请先登录");
        // }

        // let auth = await this.getAuth();
        //
        // this.ctx.response.set('auth', JSON.stringify(auth))
    }

    __after() {

    }

    __call() {

    }

    /**
     * 检查是否登录
     * @returns {boolean}
     */
    checkIsLogin() {
        const loginUserInfoKey = this.cookie(EnumLoginUserInfo.key);

        return loginUserInfoKey ? true : false;
    }

    /**
     * 获取缓存用户数据
     * @returns {Promise<any>}
     */
    async getCacheUserInfo() {
        const loginUserInfoKey = this.cookie(EnumLoginUserInfo.key);
        return await this.cacheService.get(loginUserInfoKey).catch(err => {
            return think.isError(err) ? err : new Error(err)
        });
    }

};
